<?php

class Ctrl_mahasiswa extends CI_Controller {

	function __construct() {
		parent::__construct();
		if (!$this->session->userdata('adminin')) {
			redirect(base_url() . 'admin/home');
		}
		$this->load->model('Model_mahasiswa');
	}

	function index() {
		$this->cari();
	}

	function form_input() {
		$data['row'] = '';
		$data['url'] = '';
		$data['title'] = 'Input Mahasiswa';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/form_input');
		$this->load->view('admin/admin_footer_view');
	}

	function form_edit($fakultas, $id) {
		$row = $this->Model_mahasiswa->select(array('idmhs' => $id));
		$data['row'] = $row[0];
		$data['fakultas'] = $fakultas;
		$data['title'] = 'Edit Mahasiswa';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/form_input', $data);
		$this->load->view('admin/admin_footer_view');
	}

	function insert() {
		$nim = $this->input->post('nim');
		if ($nim != '') {
			$this->Model_mahasiswa->insert($_POST);
			redirect(base_url() . 'admin/ctrl_mahasiswa');
		} else {
			$content['pesan'] = 'Harap nim diisi, <a href="javascript:back()">kembali</a>';
			$data['title'] = 'Pesan error';
			$this->load->view('admin/admin_header_view', $data);
			$this->load->view('admin/hal_error', $content);
			$this->load->view('admin/admin_footer_view');
		}
	}

	function uploadcsv() {
		$data['title'] = 'Upload Data Mahasiswa';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/csv_mahasiswa', array('error' => ''));
		$this->load->view('admin/admin_footer_view');
	}

	function uploadspp() {
		$data['title'] = 'Upload Data Mahasiswa';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/spp_mahasiswa', array('error' => ''));
		$this->load->view('admin/admin_footer_view');
	}

	function sppupload() {
		$tipe = $this->input->post('tipe');
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'xlsx';
		$this->load->library('upload', $config);
                $display['jumlah'] = '';
                $display['content'] = '';
		if (!$this->upload->do_upload('file')) {
			$error = array('error' => $this->upload->display_errors());
			$this->load->view('admin/admin_header_view',$display);
			$this->load->view('admin/upload_status', $error);
			$this->load->view('admin/admin_footer_view');
		} else {
			$data = array('upload_data' => $this->upload->data());
			$namafile = $data['upload_data']['full_path'];
			$row = 1;
			if (($handle = fopen($namafile, "r")) !== FALSE) {
				$this->load->library('table');
				$this->table->set_template(array('table_open' => '<table width="50%" border="1" cellpadding="4" cellspacing="3">'));
				$this->table->set_heading('NIM', 'SPP', 'Hasil');
                                // php excel bermain
                                $this->load->library('phpexcel/PHPExcel/IOFactory');
                                $this->load->library('phpexcel/PHPExcel');
                                $iofactory = new IOFactory();
                                $objphpexcel = $iofactory->load($namafile);
                                $objworksheet = $objphpexcel->getActiveSheet();
                                $highestrow = $objworksheet->getHighestRow();
                                for ($i=2;$i<=$highestrow;$i++)
                                {
                                    $this->load->model('Model_mahasiswa');
                                    $this->Model_mahasiswa->nim = $objworksheet->getCellByColumnAndRow(0,$i)->getValue();
                                    $this->Model_mahasiswa->spp = $objworksheet->getCellByColumnAndRow(1,$i)->getValue();
                                    $this->Model_mahasiswa->updatespp($this->Model_mahasiswa->nim,array('spp'=>$this->Model_mahasiswa->spp));
                                    $this->table->add_row($this->Model_mahasiswa->nim, $this->Model_mahasiswa->spp, 'Update');
                                }
                                
                                
//				$i = 0;
//				while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
//					$num = count($data);
//					if ($num != 2) {
//						$this->table->add_row('Gagal', 'Gagal', 'Gagal');
//					} else {
//						$this->load->model('Model_mahasiswa');
//						$this->Model_mahasiswa->nim = $data[0];
//						$this->Model_mahasiswa->spp = $data[1];
//						$this->Model_mahasiswa->updatespp($data[0]);
//						$this->table->add_row($data[0], $data[1], 'Update');
//					}
//				}
//				fclose($handle);
			}
			$display['jumlah'] = $i-1;
			$display['content'] = $this->table->generate();
			$data['title'] = 'Hasil Upload';
			$this->load->view('admin/admin_header_view', $data);
			$this->load->view('admin/upload_status', $display);
			$this->load->view('admin/admin_footer_view');
		}
	}

	function cari() {
		$data['title'] = 'Daftar Fakultas';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/daftar_fakultas');
		$this->load->view('admin/admin_footer_view');
	}

	function daftar_mahasiswa($fakultas) {
		$cond = array('fakultas'=>$fakultas);
		$data['row'] = $this->Model_mahasiswa->select_mahasiswa_fakultas($cond);
		$data['fakultas'] = $fakultas;
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/cari_mahasiswa', $data);
		$this->load->view('admin/admin_footer_view');
	}

	function delete_mahasiswa($fakultas,$id) {
		$this->db->delete('mahasiswa', array('idmhs' => $id));
		redirect(base_url().'admin/ctrl_mahasiswa/daftar_mahasiswa/'.$fakultas);
	}

	function do_upload() {
		$tipe = $this->input->post('tipe');
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'xlsx';
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('file')) {
			$error = array('error' => $this->upload->display_errors());
			$this->load->view('admin/admin_header_view');
			$this->load->view('admin/csv_mahasiswa', $error);
			$this->load->view('admin/admin_footer_view');
		} else {
			$data = array('upload_data' => $this->upload->data());
			$namafile = $data['upload_data']['full_path'];
                        if (($handle = fopen($namafile, "r")) !== FALSE) 
                            {
                                // tabel
                            $this->load->library('table');
                            $this->table->set_template(array('table_open' => '<table align="center" width="50%" border="1" cellpadding="4" cellspacing="3">'));
                            $this->table->set_heading('NIM', 'Nama', 'Fakultas', 'Prodi', 'Angkatan', 'Asal', 'Semester', 'Slot Beasiswa', 'Status tunda', 'SPP', 'Hasil');

                                // php excel bermain
                            $this->load->library('phpexcel/PHPExcel/IOFactory');
                            $this->load->library('phpexcel/PHPExcel');
                            $iofactory = new IOFactory();
                            $objphpexcel = $iofactory->load($namafile);
                            $objworksheet = $objphpexcel->getActiveSheet();
                            $highestrow = $objworksheet->getHighestRow();
                            for ($i=2;$i<=$highestrow;$i++)
                                {
                                $this->load->model('Model_mahasiswa');
                                $this->Model_mahasiswa->nim = $objworksheet->getCellByColumnAndRow(0,$i)->getValue();
                                $this->Model_mahasiswa->nama = $objworksheet->getCellByColumnAndRow(1,$i)->getValue();
                                $this->Model_mahasiswa->fakultas = strtolower($objworksheet->getCellByColumnAndRow(2,$i)->getValue());
                                $this->Model_mahasiswa->prodi = $objworksheet->getCellByColumnAndRow(3,$i)->getValue();
                                $this->Model_mahasiswa->angkatan = $objworksheet->getCellByColumnAndRow(4,$i)->getValue();
                                $this->Model_mahasiswa->asal = $objworksheet->getCellByColumnAndRow(5,$i)->getValue();
                                $this->Model_mahasiswa->semester = $objworksheet->getCellByColumnAndRow(6,$i)->getValue();
                                $this->Model_mahasiswa->slotbeasiswa = $objworksheet->getCellByColumnAndRow(7,$i)->getValue();
                                $this->Model_mahasiswa->statustunda = $objworksheet->getCellByColumnAndRow(8,$i)->getValue();
                                $this->Model_mahasiswa->spp = $objworksheet->getCellByColumnAndRow(9,$i)->getValue();
                                $kondisi = array('nim' => $this->Model_mahasiswa->nim);
                                $query = $this->Model_mahasiswa->select($kondisi);
                                if (count($query) == 0) 
                                        {
                                        $this->Model_mahasiswa->insert();
                                        $this->table->add_row($this->Model_mahasiswa->nim, $this->Model_mahasiswa->nama, $this->Model_mahasiswa->fakultas
                                                , $this->Model_mahasiswa->prodi, $this->Model_mahasiswa->angkatan, $this->Model_mahasiswa->asal, $this->Model_mahasiswa->semester
                                                , $this->Model_mahasiswa->slotbeasiswa, $this->Model_mahasiswa->statustunda, $this->Model_mahasiswa->spp, 'Sukses');
                                        $i++;
                                        } else {
                                        if ($tipe == 1) {
                                                $this->table->add_row($this->Model_mahasiswa->nim, $this->Model_mahasiswa->nama, $this->Model_mahasiswa->fakultas
                                                , $this->Model_mahasiswa->prodi, $this->Model_mahasiswa->angkatan, $this->Model_mahasiswa->asal, $this->Model_mahasiswa->semester
                                                , $this->Model_mahasiswa->slotbeasiswa, $this->Model_mahasiswa->statustunda, $this->Model_mahasiswa->spp, 'Skip');
                                                } else {
                                                $this->Model_mahasiswa->update($data[0]);
                                                $this->table->add_row($this->Model_mahasiswa->nim, $this->Model_mahasiswa->nama, $this->Model_mahasiswa->fakultas
                                                , $this->Model_mahasiswa->prodi, $this->Model_mahasiswa->angkatan, $this->Model_mahasiswa->asal, $this->Model_mahasiswa->semester
                                                , $this->Model_mahasiswa->slotbeasiswa, $this->Model_mahasiswa->statustunda, $this->Model_mahasiswa->spp, 'Update');
                                                }
                                        }
                                }
                            }
                        
//			if (($handle = fopen($namafile, "r")) !== FALSE) {
//				$this->load->library('table');
//				$this->table->set_template(array('table_open' => '<table align="center" width="50%" border="1" cellpadding="4" cellspacing="3">'));
//				$this->table->set_heading('NIM', 'Nama', 'Fakultas', 'Prodi', 'Angkatan', 'Asal', 'Semester', 'Slot Beasiswa', 'Status tunda', 'SPP', 'Hasil');
//				$i = 0;
//				while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
//					$num = count($data);
//					if (false) {
//						$this->table->add_row('Gagal', 'Gagal', 'Gagal', 'Gagal', 'Gagal', 'Gagal', 'Gagal', 'Gagal', 'Gagal', 'Gagal', 'Gagal');
//					} else {
//						$this->load->model('Model_mahasiswa');
//						$this->Model_mahasiswa->nim = $data[0];
//						$this->Model_mahasiswa->nama = $data[1];
//						$this->Model_mahasiswa->fakultas = strtolower($data[2]);
//						$this->Model_mahasiswa->prodi = $data[3];
//						$this->Model_mahasiswa->angkatan = $data[4];
//						$this->Model_mahasiswa->asal = $data[5];
//						$this->Model_mahasiswa->semester = $data[6];
//						$this->Model_mahasiswa->slotbeasiswa = $data[7];
//						$this->Model_mahasiswa->statustunda = $data[8];
//						$this->Model_mahasiswa->spp = $data[9];
//						$kondisi = array('nim' => $data[0]);
//						$query = $this->Model_mahasiswa->select($kondisi);
//						if (count($query) == 0) {
//							$this->Model_mahasiswa->insert();
//							$this->table->add_row($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], 'Sukses');
//							$i++;
//						} else {
//							if ($tipe == 1) {
//								$this->table->add_row($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], 'Skip');
//							} else {
//								$this->Model_mahasiswa->update($data[0]);
//								$this->table->add_row($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], 'Update');
//							}
//						}
//					}
//				}
//				fclose($handle);
//			}
			$display['jumlah'] = $i-1;
			$display['content'] = $this->table->generate();
			$data['title'] = 'Daftar Mahasiswa';
			$this->load->view('admin/admin_header_view', $data);
			$this->load->view('admin/upload_status', $display);
			$this->load->view('admin/admin_footer_view');
		}
	}

	function edit($fakultas, $id) {
		$nim = $this->input->post('nim');
		if ($nim != '') {
			$this->Model_mahasiswa->update($nim, $_POST);
			redirect($url);
		} else {
			$content['pesan'] = 'Harap nim diisi, <a href="javascript:back()">kembali</a>';
			$data['title'] = 'Pesan error';
			$this->load->view('admin/admin_header_view', $data);
			$this->load->view('admin/hal_pesan', $content);
			$this->load->view('admin/admin_footer_view');
		}
	}

	function updatesemester() {
		$query = 'update mahasiswa set semester=semester+1';
		$this->db->query($query);
		$query = 'update mahasiswa set slotbeasiswa=slotbeasiswa-1';
		$this->db->query($query);
		$content['pesan'] = 'Semester baru telah dijalankan';
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/hal_pesan', $content);
		$this->load->view('admin/admin_footer_view');
	}

	function profil_mahasiswa($id) {
		$where = array('idmhs' => $id);
		$row = $this->Model_mahasiswa->select($where, '', '');
		$data['row'] = $row[0];
		$beasiswa = $this->db->get_where('donasi', array('idmhs' => $id));
		$data['beasiswa'] = $beasiswa->result_array();
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/profil_mahasiswa', $data);
		$this->load->view('admin/admin_footer_view');
	}
}